package com.todo.biz.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.application.convert.BizBusinessConvert;
import com.todo.biz.application.entity.bo.BizCounterBO;
import com.todo.biz.application.service.BizCounterService;
import com.todo.biz.common.enums.CounterTypeEnum;
import com.todo.biz.infrastructure.entity.po.BizCounterPO;
import com.todo.biz.infrastructure.repository.BizCounterRepository;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.common.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (BizCounter)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:44
 */
@Slf4j
@Service("bizCounterService")
public class BizCounterServiceImpl implements BizCounterService {

    @Resource
    private BizCounterRepository counterRepository;

    @Override
    public Boolean insertBizCounter(BizCounterBO bizCounterBO) {
        if (bizCounterBO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<BizCounterBO> bizCounterBOList = queryBizCounterByBizIdAndScene(bizCounterBO.getBusinessId(), bizCounterBO.getBusinessScene());
        if (CollectionUtils.isNotEmpty(bizCounterBOList)) {
            for (BizCounterBO alreadyData : bizCounterBOList) {
                if (alreadyData.getCounterType().equals(bizCounterBO.getCounterType())) {
                    log.info("BizCounterService#insertBizCounter 新增计数数据完成，存在当前场景下类型数据，默认成功 bizCounterBO：【{}】", JSONObject.toJSONString(bizCounterBO));
                    return true;
                }
            }
        }
        BizCounterPO bizCounterPO = BizBusinessConvert.convertToBizCounterPO(bizCounterBO);
        Boolean insertResult = counterRepository.insertBizCounterPO(bizCounterPO);
        return insertResult;
    }

    @Override
    public Boolean updateBizCounter(BizCounterBO bizCounterBO) {
        if (bizCounterBO == null || bizCounterBO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        BizCounterBO alreadyData = queryBizCounterById(bizCounterBO.getId());
        if (alreadyData == null) {
            log.error("BizCounterService#updateBizCounter 更新计数数据完成，数据为空，默认不处理 bizCounterBO：【{}】", JSONObject.toJSONString(bizCounterBO));
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!alreadyData.getCounterType().equals(bizCounterBO.getCounterType())
                || !alreadyData.getBusinessId().equals(bizCounterBO.getBusinessId())
                || !alreadyData.getBusinessScene().equals(bizCounterBO.getBusinessScene())) {
            log.error("BizCounterService#updateBizCounter 更新计数数据完成，数据与传入参数不匹配，默认不处理 bizCounterBO：【{}】", JSONObject.toJSONString(bizCounterBO));
            throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION);
        }

        if (alreadyData.getLastCalTime() != null) {
            if (alreadyData.getLastCalTime().after(bizCounterBO.getLastCalTime())) {
                log.error("BizCounterService#updateBizCounter 更新计数数据完成，传入时间小于数据库时间，默认不处理 bizCounterBO：【{}】", JSONObject.toJSONString(bizCounterBO));
                throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION);
            }
        }
        BizCounterPO bizCounterPO = BizBusinessConvert.convertToBizCounterPO(bizCounterBO);
        Boolean updateResult = counterRepository.updateBizCounterPOById(bizCounterPO);
        return updateResult;
    }

    @Override
    public Boolean removeBizCounterById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        BizCounterBO bizCounterBO = queryBizCounterById(id);
        if (bizCounterBO == null) {
            return true;
        }
        return counterRepository.removeBizCounterPOById(id);
    }

    @Override
    public BizCounterBO queryBizCounterById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        BizCounterPO bizCounterPO = counterRepository.queryBizCounterPOById(id);
        return BizBusinessConvert.convertToBizCounterBO(bizCounterPO);
    }

    @Override
    public List<BizCounterBO> queryBizCounterByBizIdAndScene(Long bizId, BusinessSceneEnum scene) {
        if (bizId == null || bizId < 0 || scene == null) {
            return Collections.emptyList();
        }
        List<BizCounterPO> bizCounterPOList = counterRepository.queryBizCounterListByType(bizId, scene.getScene());
        if (CollectionUtils.isEmpty(bizCounterPOList)) {
            log.warn("BizCounterService#queryBizCounterByBizIdAndScene 根据业务id和场景查询计数数据列表完成，数据为空 bizId：【{}】scene:【{}】", bizId, scene);
            return Collections.emptyList();
        }
        return bizCounterPOList.stream().map(BizBusinessConvert::convertToBizCounterBO).collect(Collectors.toList());
    }

    @Override
    public BizCounterBO queryBizCounterByBizIdAndSceneAndType(Long bizId, BusinessSceneEnum scene, CounterTypeEnum counterType) {
        if (bizId == null || bizId < 0 || scene == null || counterType == null) {
            return null;
        }
        BizCounterPO bizCounterPO = counterRepository.queryBizCounterByBizIdAndSceneAndType(bizId, scene.getScene(), counterType.getType());
        return BizBusinessConvert.convertToBizCounterBO(bizCounterPO);
    }
}
